<template>
    <div>
        <el-form label-width="110px" size="small">
            <el-row class="form-row">
                <el-col :span="12">
                    <el-form-item label="产品代码:">
                        <RiskEntryInput id="deposit-fundCode" @set-entry-data="getEntryslist" :returnDatas="instruction.fundCode" />
                    </el-form-item>
                    <el-form-item label="资产单元编号:" v-show="SHOW_SECURITY">
                        <el-select v-model="instruction.assetCode" size="mini">
                            <el-option
                                v-for="i in computedAssetCodeSelectData"
                                :key="i.assetCode"
                                :label="i.assetCode + ' ' + i.assetName"
                                :value="i.assetCode"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="组合编码:">
                        <el-select name="deposit-组合编码" v-model="instruction.fundAccountCode" size="mini" @change="handleFundAccountCodeChange">
                            <el-option
                                v-for="i in computedFundAccountCodeSelectData"
                                :key="i.fundAccountCode"
                                :label="i.fundAccountCode + ' ' + i.fundAccountName"
                                :value="i.fundAccountCode"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="业务类型:">
                        <el-select
                            name="deposit-业务类型"
                            size="mini"
                            v-model="instruction.businessType"
                            disabled
                            @change="setValues('businessType')"
                        >
                            <el-option v-for="i in businessTypeData" :key="i.dictKey" :label="i.dictKey + ' ' + i.dictKeyName" :value="i.dictKey">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="委托方向:">
                        <el-select
                            name="deposit-委托方向"
                            size="mini"
                            v-model="instruction.orderDirection"
                            :disabled="!isMesh"
                            :class="['font-color-' + 'red']"
                            @change="setValues('orderDirection')"
                        >
                            <el-option label="存款" value="F" :style="handleTextColor('F')" name="orderDirectionF"> </el-option>
                            <el-option label="存款支取" value="i" :style="handleTextColor('i')" name="orderDirectioni"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="存款类型:">
                        <el-select name="depositType" size="mini" v-model="instruction.depositType" @change="handleDepositTypeChange">
                            <el-option
                                v-for="i in depositTypeSelectData"
                                :key="i.dictKey"
                                :label="i.dictKey + ' ' + i.dictKeyName"
                                :value="i.dictKey"
                                :name="'depositType-' + i.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="目标类型:">
                        <el-select name="deposit-目标类型" v-model="instruction.targetType" size="mini" disabled>
                            <el-option v-for="(value, key) in targetTypePojo" :key="key" :label="value" :value="key"> </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="通知天数:">
                        <RiskInputNumber
                            :input="instruction.noticeDays"
                            :inputAfter="'天'"
                            :precision="0"
                            :field="'noticeDays'"
                            :disabled="noticeDaysStatus"
                            @getInputNumber="getInputNumber(arguments)"
                        >
                        </RiskInputNumber> -->
                    <!-- <el-input v-model="instruction.noticeDays" :disabled="noticeDaysStatus">
                          <template slot="append">天</template>
                        </el-input> -->
                    <!-- </el-form-item> -->
                    <el-form-item label="银行总行:">
                        <el-select
                            name="银行总行"
                            size="mini"
                            v-model="instruction.bankCode"
                            filterable
                            remote
                            :remote-method="getBankCodeList"
                            @change="setValues"
                        >
                            <el-option
                                v-for="i in bankCodeSelectData"
                                :key="i.dictKey"
                                :label="i.dictKey + ' ' + i.dictKeyName"
                                :value="i.dictKey"
                                :name="'bankCode-' + i.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- v-show="noticeDaysStatus" -->
                    <el-form-item label="提前支取限制:">
                        <el-select name="提前支取限制" size="mini" v-model="instruction.advanceLimitFlag" @change="setValues('advanceLimitFlag')">
                            <el-option
                                v-for="i in advanceLimitFlagSelectData"
                                :key="i.dictKey"
                                :label="i.dictKeyName"
                                :value="i.dictKey"
                                :name="'advanceLimitFlag-' + i.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <!-- <el-form-item label="提前支取限制:" v-show="!noticeDaysStatus">
                        <el-select size="mini" v-model="instruction.advanceLimitFlag">
                            <el-option v-for="i in advanceLimitFlagSelectData" :key="i.dictKey" :label="i.dictKeyName" :value="i.dictKey">
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item label="存款利率:">
                        <RiskInputNumber
                            :maxlength="7"
                            :input="instruction.depositRate"
                            :inputAfter="'%'"
                            :colorStyle="textColor"
                            :precision="2"
                            :field="'depositRate'"
                            @getInputNumber="getInputNumber(arguments)"
                        >
                        </RiskInputNumber>
                    </el-form-item>
                    <el-form-item label="存款金额:">
                        <RiskInputNumber
                            :maxlength="15"
                            :input="instruction.orderBalance"
                            :inputAfter="'元'"
                            :colorStyle="textColor"
                            :precision="2"
                            :field="'orderBalance'"
                            @getInputNumber="getInputNumber(arguments)"
                        >
                        </RiskInputNumber>
                        <!-- <el-input v-model="instruction.orderBalance" @change="handleOrderBalanceChange"><template slot="append">元</template></el-input> -->
                    </el-form-item>
                    <el-form-item label="起息日:"
                        ><el-date-picker
                            name="depositStartDate"
                            v-model="instruction.depositStartDate"
                            @change="handleDepositStartDateChange"
                            type="date"
                            format="yyyy-MM-dd"
                            value-format="yyyyMMdd"
                            style="width: 100%"
                            placeholder="请选择"
                            :picker-options="pickerOptions"
                        ></el-date-picker
                    ></el-form-item>
                    <el-form-item label="到期类型:" v-show="noticeDaysStatus">
                        <el-select name="mutureType" size="mini" v-model="instruction.mutureType" @change="handleMutureTypeChange">
                            <el-option
                                v-for="i in mutureTypeSelectData"
                                :key="i.dictKey"
                                :label="i.dictKey + ' ' + i.dictKeyName"
                                :value="i.dictKey"
                                :name="'mutureType-' + i.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="存款期限:" v-show="noticeDaysStatus">
                        <RiskInputNumber
                            name="depositLimitTime"
                            :maxlength="4"
                            :input="instruction.depositLimitTime"
                            :inputAfter="'月'"
                            :colorStyle="textColor"
                            :precision="0"
                            :field="'depositLimitTime'"
                            :disabled="instruction.mutureType == '2'"
                            @getInputNumber="getInputNumber(arguments)"
                        >
                        </RiskInputNumber>
                    </el-form-item>
                    <el-form-item label="支取类型:" v-show="!noticeDaysStatus">
                        <el-select name="advanceType" size="mini" v-model="instruction.advanceType" @change="handleAdvanceTypeChange">
                            <el-option
                                v-for="i in advanceTypeSelectData"
                                :key="i.dictKey"
                                :label="i.dictKey + ' ' + i.dictKeyName"
                                :value="i.dictKey"
                                :name="'advanceType-' + i.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="通知天数:" v-show="!noticeDaysStatus">
                        <el-select name="noticeDays" size="mini" v-model="instruction.noticeDays" @change="handleNoticeDaysChange">
                            <el-option
                                v-for="i in noticeDaysSelectData"
                                :key="i.dictKey"
                                :label="i.dictKeyName"
                                :value="i.dictKey"
                                :name="'noticeDays-' + i.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="到期日期:" v-show="instruction.depositType !== '2'"
                        ><el-date-picker
                            name="mutureDate"
                            v-model="instruction.mutureDate"
                            @change="handleMutureDateChange"
                            type="date"
                            format="yyyy-MM-dd"
                            value-format="yyyyMMdd"
                            style="width: 100%"
                            placeholder="请选择"
                            :picker-options="pickerOptions"
                            :disabled="instruction.mutureType == '1'"
                        ></el-date-picker
                    ></el-form-item>
                    <!-- || instruction.advanceType == '1' -->
                    <el-form-item label="支取日期:" v-show="instruction.depositType == '2'">
                        <el-date-picker
                            name="drawDate"
                            v-model="instruction.drawDate"
                            @change="handleDrawDateDateChange"
                            type="date"
                            format="yyyy-MM-dd"
                            value-format="yyyyMMdd"
                            style="width: 100%"
                            placeholder="请选择"
                            :picker-options="pickerOptions"
                            :disabled="instruction.advanceType == '1'"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="存款利息:">
                        <RiskInputNumber
                            name="depositInterest"
                            :maxlength="15"
                            :input="instruction.depositInterest"
                            :inputAfter="'元'"
                            :colorStyle="textColor"
                            :precision="2"
                            :field="'depositInterest'"
                            :disabled="true"
                            @getInputNumber="getInputNumber(arguments)"
                        >
                        </RiskInputNumber>
                        <!-- <el-input v-model="instruction.depositInterest" @change="handleDepositInterestChange"><template slot="append">元</template></el-input> -->
                    </el-form-item>
                    <el-form-item label="到期金额:">
                        <RiskInputNumber
                            name="expireBalance"
                            :input="instruction.expireBalance"
                            :inputAfter="'元'"
                            :colorStyle="textColor"
                            :precision="2"
                            :field="'expireBalance'"
                            :disabled="true"
                            @getInputNumber="getInputNumber(arguments)"
                        >
                        </RiskInputNumber>
                        <!-- <el-input v-model="instruction.expireBalance">
                        <template slot="append">元</template>
                        </el-input> -->
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script lang="ts">
import Invest from '@/common/dict/invest';
import DictionConstUtil, { objToArr, SGU_CACHE } from '@/utils/diction-const-util';
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import { getBankSelect, getStockReportSelect } from '../api';
import moment from 'moment';
import RiskInputNumber from '@/pages/risk-measure/components/risk-input-number.vue';
import RiskEntryInput from '@/common/components/risk-entry-input.vue';
import { getConfigItem, handleTextColor, uniqueFunc } from '@/utils/tools';

@Component({
    name: 'CK',
    components: {
        RiskInputNumber,
        RiskEntryInput,
    },
})

// 存款
export default class CK extends Vue {
    @Prop()
    detail: any;
    @Prop()
    stockInfoCache: any;
    @Prop()
    renderCount: number;
    @Prop({ default: false })
    isMesh: boolean;
    Invest: any = new Invest();
    handleTextColor = handleTextColor;
    fundCodeSelectData: any[] = []; // 产品序号下拉框数据
    fundAccountCodeSelectData: any[] = []; // 组合编码下拉框数据
    allStockType: any[] = [];
    allOrderDirection: any[] = [];
    // orderDirectionSelectData: any[] = []; // 订单方向下拉框数据
    depositTypeSelectData: any[] = [
        { dictKey: '1', dictKeyName: '定期存款' },
        { dictKey: '2', dictKeyName: '通知存款' },
        { dictKey: '3', dictKeyName: '协议存款' },
        { dictKey: '6', dictKeyName: '次级债务' },
    ]; // 存款类型下拉数据

    advanceLimitFlagSelectData: any[] = []; // 提前支取限制类型下拉数据

    // 通知天数下拉数据
    noticeDaysSelectData: any[] = [
        { dictKey: '1', dictKeyName: '1天' },
        { dictKey: '7', dictKeyName: '7天' },
    ];

    // 支取类型
    advanceTypeSelectData: any[] = [
        { dictKey: '1', dictKeyName: '另行通知' },
        { dictKey: '2', dictKeyName: '确定日期' },
    ];
    bankCodeSelectData: any[] = []; // 银行序号下拉框数据
    businessTypeData: any[] = [{ dictKeyName: '存款类', dictKey: 'B' }];
    mutureTypeSelectData: any[] = []; // 到期类型
    // 获取到期期限下拉数据
    stockReport: any[] = []; // 证券代码下拉框数据
    textColor: string = 'red';

    noticeDaysStatus: boolean = true; // 通知天数禁用

    // advanceTypeStatus:boolean = false;// 支取类型值引起的禁用

    instruction = {
        fundCode: '', // 产品代码
        fundAccountCode: '', // 组合编码
        reportCode: '', // 申报代码
        orderDirection: 'F', // 委托方向
        stockCode: '', // 证券代码
        depositRate: '', // 存款利率
        depositStartDate: moment().format('YYYYMMDD'), // 起息日
        depositType: '1', // 存款类型
        mutureDate: '', // 到期日期
        noticeDays: '', // 通知天数
        depositInterest: '', // 存款利息
        expireBalance: '', // 到期金额
        orderBalance: '', // 存款金额
        bankCode: '', // 银行总行
        instructionType: 'B', // 业务类型
        instructionTypeName: '存款类', // 业务类型名称
        opponentCode: '', // 交易对手编码
        businessType: 'B', // 业务类型
        advanceLimitFlag: '', // 支取类型
        fundNameSt: '',
        fundAccountName: '',
        assetCode: '',
        assetName: '',
        mutureType: '1', // 到期类型
        depositLimitTime: '', // 存款期限
        advanceType: '1', // 支取类型
        drawDate: '', // 支取日期
        targetType: '2',
    };

    stockInfo: any = {}; // 证券信息
    targetTypePojo: any = {}; // 目标类型

    pickerOptions: any = {
        disabledDate(time) {
            return time.getTime() < Date.now() - 1 * 24 * 3600 * 1000;
        },
    };

    // 委托方向对应业务类型数据
    innerOrderDirectionData: any[] = [];

    async mounted() {
        // 获取目标类型
        this.targetTypePojo = DictionConstUtil.targetType;
        // 到期类型
        this.mutureTypeSelectData = objToArr(this.Invest.mutureType);

        // 获取产品信息 (产品序号下拉框数据)
        this.fundCodeSelectData = await SGU_CACHE('FundInfo', '/fundInfo/list', []);
        // 获取组合编码下拉框数据
        let arr = await SGU_CACHE('FundAccount', '/fundInfo/accounts', []);
        arr.forEach(item => {
            this.fundAccountCodeSelectData[item.fundCode] = item.fundAccounts;
        });
        // 获取存款类型下拉数据
        this.depositTypeSelectData = objToArr(this.Invest.depositType);
        // 获取支取类型下拉数据
        this.advanceLimitFlagSelectData = objToArr(this.Invest.advanceLimitFlag);
        // 获取银行总行下拉框数据
        this.getBankCodeList('');

        // 获取所有证券类别 (用于筛选订单方向)
        this.allStockType = await SGU_CACHE('StockType', '/dict/stockType/list', []);
        // 获取所有订单方向
        this.allOrderDirection = await SGU_CACHE('TradeDirection', '/dict/tradeDirections', []);
        await this.getstockCodeList('');
        this.stockInfo = this.stockReport.find(
            item => item.reportCode === { '1': '870870', '2': '860860', '3': '890890', '6': '880880' }[this.instruction.depositType]
        );
        this.$emit('getStockInfo', this.stockInfo, '5');
        // this.pickerOptions = this.disabledDate();
    }

    @Watch('stockInfoCache', { immediate: true, deep: true })
    onWatchStockInfo() {
        if (this.stockInfoCache) {
            this.stockInfo = this.stockInfoCache;
        }
    }

    // @Watch('detail', { immediate: true, deep: true })
    @Watch('renderCount', { immediate: true, deep: true })
    async setForm() {
        if (
            JSON.stringify(this.detail) !== JSON.stringify(this.instruction) &&
            JSON.stringify(this.detail) !== JSON.stringify({ orderDirection: 'F' })
        ) {
            if (this.detail && this.detail.orderDirection == 'F') {
                // this.detail.mutureType = '2'; 测试
                let instruction = JSON.parse(JSON.stringify(this.detail));

                // 获取组合编码下拉框数据
                let arr = await SGU_CACHE('FundAccount', '/fundInfo/accounts', []);
                arr.forEach(item => {
                    this.fundAccountCodeSelectData[item.fundCode] = item.fundAccounts;
                });
                for (const key in instruction) {
                    this.instruction[key] = instruction[key];
                }
                this.instruction.targetType = '2';

                if (instruction.depositType !== '2') {
                    this.noticeDaysStatus = true;
                } else {
                    this.noticeDaysStatus = false;
                }
                // debugger;
                // 产品名称
                this.instruction.fundNameSt = instruction.fundNameSt || instruction.fundName;
                // 组合编码
                this.instruction.fundAccountCode = instruction.fundAccountCode || instruction.groupCode;
                // 组合名称
                this.instruction.fundAccountName = instruction.fundAccountName || instruction.groupName;
                // 存款金额
                this.instruction.orderBalance = instruction.orderBalance || instruction.depositBalance;
                // 到期日期
                this.instruction.mutureDate = instruction.mutureDate && instruction.mutureDate.toString();

                // 起息日
                this.instruction.depositStartDate =
                    instruction.depositStartDate ||
                    moment(instruction.mutureDate && instruction.mutureDate.toString())
                        .subtract(instruction.depositDeadline, 'days')
                        .format('YYYYMMDD');

                if (this.detail?.instNo) {
                    // 指令修改处理存款期限回显
                    if (instruction.depositDeadMonthLine && instruction.depositDeadMonthLine.indexOf('.') > -1) {
                        // 存款期限(月)字段为小数表示确定日期
                        instruction.mutureType = '2';
                    } else {
                        instruction.mutureType = '1';
                    }
                    this.instruction.mutureType = instruction.mutureType;
                }

                // 存款期限
                if (instruction.depositType != '2' && instruction.mutureType == '1' && !instruction.depositLimitTime) {
                    this.instruction.depositLimitTime = String(
                        moment(this.instruction.mutureDate).diff(moment(this.instruction.depositStartDate), 'months')
                    );
                }

                console.log(this.instruction.mutureType, 'depositLimitTime');

                // 支取日期
                if (this.instruction.drawDate && this.instruction.drawDate == '0') {
                    this.instruction.advanceType = '1';
                    this.instruction.drawDate = '';
                } else {
                    this.instruction.advanceType = '2';
                }
            }
        } else {
            console.log('同重置');
            this.instruction = {
                fundCode: '', // 产品代码
                fundAccountCode: '', // 组合编码
                reportCode: '', // 申报代码
                orderDirection: 'F', // 委托方向
                stockCode: '', // 证券代码
                depositRate: '', // 存款利率
                depositStartDate: moment().format('YYYYMMDD'), // 起息日
                depositType: '1', // 存款类型
                mutureDate: '', // 到期日期
                noticeDays: '', // 通知天数
                depositInterest: '', // 存款利息
                expireBalance: '', // 到期金额
                orderBalance: '', // 存款金额
                bankCode: '', // 银行总行
                instructionType: 'B', // 业务类型
                instructionTypeName: '存款类', // 业务类型名称
                opponentCode: '', // 交易对手编码
                businessType: 'B', // 业务类型
                advanceLimitFlag: '', // 提前支取限制
                fundNameSt: '',
                fundAccountName: '',
                assetCode: '',
                assetName: '',
                mutureType: '1', // 到期类型
                depositLimitTime: '', // 存款期限
                advanceType: '1', // 支取类型
                drawDate: '', // 支取日期
                targetType: '2',
            };
            this.stockInfo = {};
            // this.$emit('getStockInfo', this.stockInfo, '5');
        }
        if (!this.stockReport.length) {
            await this.getstockCodeList(this.detail.reportCode);
        }
        this.stockInfo = this.stockReport.find(
            item => item.reportCode === { '1': '870870', '2': '860860', '3': '890890', '6': '880880' }[this.detail.depositType]
        );
        this.$emit('getStockInfo', this.stockInfo, '5');
    }
    SHOW_SECURITY = getConfigItem('SHOW_SECURITY');
    // 监听 资产单元计算属性 (每当计算属性变动时 自动选中第一项)
    @Watch('computedAssetCodeSelectData', { immediate: true, deep: true })
    watchcomputedAssetCode(newData) {
        if (!this.SHOW_SECURITY) {
            return;
        }
        let obj: any = {};
        if (newData && newData.length) {
            const d1 = newData.find(item => item.assetCode == this.instruction.assetCode);
            obj = d1 || newData[0];
        }
        this.instruction.assetCode = obj.assetCode;
        this.instruction.assetName = obj.assetName;
    }

    // 监听 组合编码计算属性 (每当计算属性变动时 自动选中第一项)
    @Watch('computedFundAccountCodeSelectData', { immediate: true, deep: true })
    watchcomputedFundAccountCode(newData) {
        let obj: any = {};
        if (newData && newData.length) {
            const d1 = newData.find(item => item['fundAccountCode'] == this.instruction['fundAccountCode']);
            obj = d1 || newData[0];
        }
        this.instruction.assetCode = obj.assetCode;
        this.instruction.assetName = obj.assetName;
        this.instruction.fundAccountCode = obj.fundAccountCode;
        this.instruction.fundAccountName = obj.fundAccountName;
        this.setValues();
    }

    get computedAssetCodeSelectData(): any {
        return uniqueFunc(this.fundAccountCodeSelectData[this.instruction.fundCode], 'assetCode');
    }

    // 组合编码下拉框计算属性;
    get computedFundAccountCodeSelectData() {
        let data = this.fundAccountCodeSelectData[this.instruction.fundCode];
        let retData = [];
        if (data && this.SHOW_SECURITY) {
            const obj = data.filter(item => item.assetCode == this.instruction.assetCode);
            retData = obj || [];
        } else {
            retData = data;
        }
        return retData;
    }

    // @Watch('instruction', { immediate: true, deep: true })
    setValues(param?: string) {
        let value =
            param == 'orderDirection'
                ? {
                      fundCode: this.instruction.fundCode,
                      fundNameSt: this.instruction.fundNameSt,
                      fundAccountCode: this.instruction.fundAccountCode,
                      fundAccountName: this.instruction.fundAccountName,
                      assetCode: this.instruction.assetCode,
                      assetName: this.instruction.assetName,
                      orderDirection: 'i',
                  }
                : this.instruction;

        this.$emit('handleSettingValue', value, '5');
    }

    getInputNumber(args: any) {
        let value = args[0];
        let field = args[1];
        this.instruction[field] = value;
        if (field == 'depositRate') {
            this.handleDepositRateChange(value);
        } else if (field == 'orderBalance') {
            this.handleOrderBalanceChange(value);
        } else if (field == 'depositInterest') {
            this.handleDepositInterestChange(value);
        } else if (field == 'depositLimitTime') {
            this.handleDepositLimitTimeChange(value);
        }
        this.setValues();
    }

    getBankCodeList(value: string) {
        getBankSelect({
            dictKey: value,
            dictKeyName: value,
        }).then((res: any) => {
            this.bankCodeSelectData = res;
        });
    }

    // 获取证券代码下拉框数据
    getstockCodeList(value: string) {
        return new Promise((resolve, reject) => {
            this.$axios
                .post('/dict/stock/report/select/' + 6, {
                    dictKey: value,
                    dictKeyName: value,
                })
                .then((res: any) => {
                    this.stockReport = res || [];
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });
        });
    }

    getEntryslist(val: any) {
        this.instruction.fundCode = val;
        this.handleFundCodeChange(val);
    }

    handleFundCodeChange(val: string): void {
        let fund = this.fundCodeSelectData.find(item => item.fundCode === val) || {};
        this.instruction.fundNameSt = fund.fundNameSt;
        let fundAccount = (this.fundAccountCodeSelectData[this.instruction.fundCode] || [])[0] || {};
        this.instruction.fundAccountCode = fundAccount.fundAccountCode;
        this.instruction.fundAccountName = fundAccount.fundAccountName;
        this.instruction.assetCode = fundAccount.assetCode;
        this.instruction.assetName = fundAccount.assetName;
        this.setValues();
    }

    handleFundAccountCodeChange(value: string): void {
        let fundAccount = (this.fundAccountCodeSelectData[this.instruction.fundCode] || []).find(item => item.fundAccountCode === value) || {};
        this.instruction.fundAccountName = fundAccount.fundAccountName;
        this.instruction.assetCode = fundAccount.assetCode;
        this.instruction.assetName = fundAccount.assetName;
        this.setValues();
    }

    handleMutureDateChange(val: string): void {
        this.instruction.depositInterest = this.calcDepositInterest(this.instruction);
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
        if (this.instruction.depositStartDate && this.instruction.mutureDate) {
            let beginDate = new Date(this.instruction.depositStartDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3'));
            let endDate = new Date(this.instruction.mutureDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3'));
            if (beginDate > endDate) {
                this.yh_message_warn('起息日期不能大于到期日期');
            }
        }
        this.setValues();
    }

    handleDrawDateDateChange(val: string): void {
        this.instruction.depositInterest = this.calcDepositInterest(this.instruction);
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
        if (this.instruction.depositStartDate && this.instruction.drawDate) {
            let beginDate = new Date(this.instruction.depositStartDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3'));
            let endDate = new Date(this.instruction.drawDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3'));
            if (beginDate > endDate) {
                this.yh_message_warn('起息日期不能大于到期日期');
            }
        }
        this.setValues();
    }

    handleDepositStartDateChange(val: string): void {
        this.instruction.depositInterest = this.calcDepositInterest(this.instruction);
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
        if (this.instruction.depositStartDate && this.instruction.mutureDate) {
            let beginDate = new Date(this.instruction.depositStartDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3'));
            let endDate = new Date(this.instruction.mutureDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3'));
            if (beginDate > endDate) {
                this.yh_message_warn('起息日期不能大于到期日期');
                //
            }
        }
        this.setValues();
    }

    handleOrderBalanceChange(val: string): void {
        this.instruction.depositInterest = this.calcDepositInterest(this.instruction);
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
    }

    // 存款类型
    handleDepositTypeChange(val: string): void {
        this.instruction.noticeDays = '';
        this.stockInfo = this.stockReport.find(item => item.reportCode === { '1': '870870', '2': '860860', '3': '890890', '6': '880880' }[val]);
        this.$emit('getStockInfo', this.stockInfo, '5');

        if (val !== '2') {
            // 1 协议存款  3 定期存款
            this.noticeDaysStatus = true;
            this.instruction.drawDate = '';
            this.instruction.mutureType = this.instruction.mutureType || '1';
            // this.handleDepositLimitTimeChange(this.instruction.depositLimitTime);
            this.instruction.mutureDate = this.instruction.depositLimitTime ? this.instruction.mutureDate : '';
            this.handleMutureDateChange(this.instruction.mutureDate);
        } else {
            // 2 通知存款
            this.noticeDaysStatus = false;
            this.instruction.noticeDays = '1';
            this.instruction.mutureType = '';
            this.instruction.depositLimitTime = '';
            this.instruction.mutureDate = '';
            this.handleMutureDateChange(this.instruction.mutureDate);
        }
        this.setValues();
    }

    handleDepositInterestChange(val: string): void {
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
    }

    handleDepositLimitTimeChange(val: string) {
        this.instruction.mutureDate = val ? moment(this.instruction.depositStartDate).add(val, 'months').format('YYYYMMDD') : '';
        this.instruction.depositInterest = this.calcDepositInterest(this.instruction);
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
    }

    handleDepositRateChange(val: string): void {
        this.instruction.depositInterest = this.calcDepositInterest(this.instruction);
        this.instruction.expireBalance = this.calcExpireBalance(this.instruction).toString();
    }

    handleMutureTypeChange(val: string) {
        if (val == '1') {
            // 确定期限
            this.instruction.mutureDate = '';
            this.handleMutureDateChange(this.instruction.mutureDate);
        } else {
            // 确定日期
            this.instruction.depositLimitTime = '';
        }
        this.setValues();
    }

    handleAdvanceTypeChange(val: string) {
        if (val == '1') {
            // 另行通知
            this.instruction.drawDate = '';
            this.handleDrawDateDateChange(this.instruction.drawDate);
        }

        this.setValues();
    }

    handleNoticeDaysChange(val: string) {
        this.setValues();
    }

    /**
     * 存款利息计算公式 存款利息=本金×实际天数×日利率
     */
    calcDepositInterest(instruction) {
        let date = '';
        if (instruction.depositType == '2') {
            date = instruction.drawDate;
        } else {
            date = instruction.mutureDate;
        }

        if (!date || !instruction.depositStartDate) {
            return '';
        }
        return String(
            (
                (Number(instruction.orderBalance || 0) *
                    Number(moment(date, 'YYYYMMDD').diff(moment(instruction.depositStartDate, 'YYYYMMDD'), 'days')) *
                    Number(instruction.depositRate || 0)) /
                100 /
                360
            ).toFixed(2)
        );
    }
    /**
     * 到期金额计算公式
     */
    calcExpireBalance(instruction) {
        return (Number(instruction.orderBalance || 0) + Number(instruction.depositInterest)).toFixed(2);
    }
}
</script>
<style lang="scss" scoped>
@import '@/pages/risk-measure/style/measure';
.form-row {
    margin-top: 3px;
    .el-form-item--small {
        margin-bottom: 3px !important;
        & /deep/ .el-form-item__label {
            color: #575757;
        }
        & /deep/ .el-input__inner {
            height: 28px;
            line-height: 28px;
        }
        .font-color-red {
            /deep/ input {
                color: red !important;
            }
            /deep/ .el-input-group__append {
                color: red !important;
            }
        }
    }
}
</style>
